package com.util;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Component
public class CourseUtil {
    /**
     * 随机生成6位或5位加课码
     * 加课码组成：课程id+2位随机数字+2位随机字母
     */
    public String generateCourseCode(String id){
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        code.append(id);

        String charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int charSetLength = charSet.length();

        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(charSetLength);
            code.append(charSet.charAt(index));
        }

        return code.toString();
    }

    public List<String> string2List(String string){

        String[] array = string.split("/");
        int length = array.length;

        return new ArrayList<>(Arrays.asList(array).subList(0, length));
    }

    public String list2String(List<String> list){
        StringBuilder string = new StringBuilder();
        for (String s : list) {
            if (s.equals("")){
                continue;
            }
            string.append(s);
            string.append("/");
        }
        return string.toString();
    }
}
